/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.itver.graphics.controller;

import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Vector3f;


/**
 *
 * @author Karo
 */
//Esta clase manipula un TransformGroup dado para el control del movimiento
//de la cámara

public class TransformGroupManipulator{
    private double angle;
    private double mov;
    private double factor;
    private TransformGroup targetTG; //TransformGroup compartido para manipular la camara
    private Transform3D t3d;
    private Vector3f translate;

    public TransformGroupManipulator(TransformGroup targetTG){
        this.targetTG = targetTG;
        translate = new Vector3f();
        t3d = new Transform3D();
        //************Estos tres valores son los defaults pero los debe tomar
        angle = 0;    //del archivo, no deben quedarse definidos aquí
        mov = 3;
        factor = 0.25;
        //*****************************************************************
    }

    public void stepForward(){
        getTransform();
        translate.z += -factor * Math.cos(Math.toRadians(angle));
        translate.x += -factor * Math.sin(Math.toRadians(angle));
        setTransform();
    }

    public void stepBackward(){
        getTransform();
        translate.z += factor * Math.cos(Math.toRadians(angle));
        translate.x += factor * Math.sin(Math.toRadians(angle));
        setTransform();
    }

    public void stepLeftward(){
        getTransform();
        translate.x += -factor * Math.cos(Math.toRadians(angle));
        translate.z += factor * Math.sin(Math.toRadians(angle));
        setTransform();
    }

    public void stepRightward(){
        getTransform();
        translate.x += factor * Math.cos(Math.toRadians(angle));
        translate.z += -factor * Math.sin(Math.toRadians(angle));
        setTransform();
    }

    public void riseUp(){
        getTransform();
        translate.y += factor;
        setTransform();
    }

    public void goDown(){
        getTransform();
        translate.y += -factor;
        setTransform();
    }

    public void turnLeft(){
        getTransform();
        Transform3D rot = new Transform3D();
        angle+=mov;
        rot.rotY(Math.toRadians(mov));
        t3d.mul(rot);
        setTransform();
    }

    public void turnRight(){
        getTransform();
        Transform3D rot = new Transform3D();
        angle -= mov;
        rot.rotY(Math.toRadians(-mov));
        t3d.mul(rot);
        setTransform();
    }

    private void getTransform(){
        targetTG.getTransform(t3d);
        t3d.get(translate);
    }

    private void setTransform(){
        t3d.setTranslation(translate);
        targetTG.setTransform(t3d);
    }

    public double getAngle() {
        return angle;
    }

    public void setAngle(double angle) {
        this.angle = angle;
    }

    public double getFactor() {
        return factor;
    }

    public void setFactor(double factor) {
        this.factor = factor;
    }

    public double getMov() {
        return mov;
    }

    public void setMov(double mov) {
        this.mov = mov;
    }

    public Transform3D getT3d() {
        return t3d;
    }

    public void setT3d(Transform3D t3d) {
        this.t3d = t3d;
    }

    public TransformGroup getTargetTG() {
        return targetTG;
    }

    public void setTargetTG(TransformGroup targetTG) {
        this.targetTG = targetTG;
    }

    public Vector3f getTranslate() {
        return translate;
    }

    public void setTranslate(Vector3f translate) {
        this.translate = translate;
    }




}
